import datetime
from applications.extensions import db
from sqlalchemy import UniqueConstraint





#报溢
class SalesRawYPSoldModel(db.Model):
    __tablename__ = '原料_yp_销售'
    id = db.Column(db.Integer, primary_key=True, comment='ID') #默认自增primary_key
    excess_dh = db.Column(db.String(255), comment='销售单号', nullable=False)
    location_id = db.Column(db.Integer,db.ForeignKey("原料_yp_按货位入库.id"), comment='入库_编号', nullable=False) # 属性 外键
    sold_number = db.Column(db.Numeric(16,4), comment='销售数量 保留小数点后4位',nullable=False)
    type = db.Column(db.String(255), default='销售单', comment='销售单')
    date_time = db.Column(db.Date, default=datetime.date.today(), comment='单据日期')
    user_solder_id = db.Column(db.Integer, db.ForeignKey("admin_user.id"), comment='销售业务人员_编号，不是销售内勤', nullable=False)

    remark = db.Column(db.String(255), comment='备注')
    create_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
    update_at = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now,
                          comment='更新时间')

    location = db.relationship('StoreRawYPModel_location', backref=db.backref('storeRawYpSold'), uselist=False)
    user_solder = db.relationship('User', foreign_keys=[user_solder_id], backref=db.backref('storeRawYpSold_solder'),uselist=False)


    # #创建联合索引
    # __table_args__ = (
    #     UniqueConstraint('代码', '版本号', name='unix_raw_yp_code_version'),
    # )
    def to_dict(self):
        return {column.name: getattr(self, column.name) for column in self.__table__.columns}